$(function () {
    $(".searchbtn").on("click", function () {
        $(".searchbox").show()
    })
    $(".cancel").on("click", function () {
        $(".searchbox").hide()
    })
    // 判断用户是否登录
    let login = JSON.parse(localStorage.getItem("login"))
    if (login) {
        $(".loginBtg").prop("href", "javascript:;").on("click", function () {
            $(".loginBox").toggle()
        })
    }
    // 退出按钮
    $(".quitBtn").on("click", function () {
        login = false
        localStorage.setItem("login", JSON.stringify(false))
        $(".loginBox").hide()
        $(".loginBtg").prop("href", "./login.html")
        window.location.href = `./index.html`;
    })
    // 页面滚动
    $(window).on("scroll", function () {
        if ($(document).scrollTop() > 600) {
            $(".elevator").show()
        } else {
            $(".elevator").hide()
        }
    })
    $(".elevator").on("click", function () {
        $('body,html').stop().animate({
            scrollTop: 0
        })
    })
    // 购物车数量
    liaNum()

    function liaNum() {
        let login = JSON.parse(localStorage.getItem("login"))
        if (login) {
            let teaCar = JSON.parse(localStorage.getItem("teaCar"))
            if(teaCar===null){
                teaCar=[]
            }
            if (teaCar.length === 0) {
                $(".carBtn>span").hide()
            } else {
                let carNum = 0
                for (const k in teaCar) {
                    if (teaCar[k].user === login.user) {
                        carNum++
                        if (carNum > 0) {
                            $(".carBtn>span").show().text(carNum)
                        } else {
                            $(".carBtn>span").hide()
                        }
                    }
                }
            }
        }
    }

    // 小购物车显示与隐藏
    $(".carBtn").on({
        mouseover: function () {
            if (login) {
                let teaCar = JSON.parse(localStorage.getItem("teaCar"))
                if(teaCar===null){
                    teaCar=[]
                }
                if (teaCar.length > 0) {
                    let login = JSON.parse(localStorage.getItem("login"))
                    for (const k in teaCar) {
                        if (teaCar[k].user === login.user) {
                            if (teaCar.length > 0) {
                                $(".carLis").show()
                            } else {
                                $(".carLis").hide()
                            }
                        }
                    }
                } else {
                    $(".carLis").hide()
                }
            }
        },
        mouseleave: function () {
            $(".carLis").hide()
        }
    })
    $(".carBtn").on("click", function () {
        let login = JSON.parse(localStorage.getItem("login"))
        if (login) {
            // window.location.href = `./data.html`;
        } else {
            window.location.href = `./login.html`;
        }
    })
    // 小购物车内容
    apply()

    function apply() {
        $(".carLis").empty()
        let teaCar = JSON.parse(localStorage.getItem("teaCar"))
        let login = JSON.parse(localStorage.getItem("login"))
        for (const k in teaCar) {
            if (teaCar[k].user === login.user) {
                let str = `
        <li>
                <img src="${teaCar[k].img}" alt="">
                <div class="carInfo">
                    <p>
                    <h5>${teaCar[k].name}</h5>
                    <span class="delTea">×</span></p>
                    <p><span>${teaCar[k].num}</span>×<span>￥${teaCar[k].bill}</span></p>
                </div>
            </li>
        `
                $(".carLis").append(str)
            }
        }
    }

    // lis删除按钮
    $(".carLis").on("click", "li", function () {
        let teaCar = JSON.parse(localStorage.getItem("teaCar"))
        teaCar.splice($(this).index(), 1)
        localStorage.setItem("teaCar", JSON.stringify(teaCar))
        apply()
        liaNum()
        if (teaCar.length === 0) {
            $(".carLis").hide()
        }
    })
})